const path = require('path')
const fs = require('fs')

const flag = 'sep' // separate

// /<style[\s\S]*?>([\s\S]*?)(?=<\/style>)/ 匹配style内容
// /<style[\s\S]*?less=[\'|\"](.*?)[\'|\"][\s\S]*?/ 匹配style格式
// /(<style[\s\S]*?>)/ 匹配style头

const styleContentReg = /<style[\s\S]*?>([\s\S]*?)(?=<\/style>)/
const typeReg = /<style[\s\S]*?less=[\'|\"](.*?)[\'|\"][\s\S]*?>/
const styleHeadReg = /(<style[\s\S]*?>)/
// const excludeStyleReg = /([\s\S]*?)(?=<style[\s\S]*?>)/

function readFile(pathData) {
    if(!pathData) return errorHandler(`readFile path error：${pathData}`)
    fs.readFile(pathData, (err, data) => {
        if (err) return errorHandler(err)
        const pathInfo = path.parse(pathData)
        // console.log('path',pathInfo)
        const fileData = data.toString()
        const styleContentInfo = getStyleContent(fileData)
        const typeInfo = getType(fileData)

        if (!styleContentInfo || !typeInfo) return
        const styleContent = `${fileData.substring(0, styleContentInfo.index)}${getStyleHead(fileData).data}\n@import url("./${pathInfo.name}-${flag}.${typeInfo.data}");\n</style>`
        
        createFile(pathInfo.dir, `${pathInfo.name}-${flag}.${typeInfo.data}`, styleContentInfo.data)
        // createFile(pathData, `${pathInfo.base}`, `${getExcludeStyleContent(fileData).data}\n`)
        createFile(pathInfo.dir, `${pathInfo.base}`, styleContent)
    })
}

function getExcludeStyleContent(data) {
    const retVal = excludeStyleReg.exec(data)
    if (!retVal) return errorHandler('getExcludeStyleContent Error')
    return {
        data: retVal[1],
        index: retVal.index
    }
}

function getStyleHead(data) {
    const retVal = styleHeadReg.exec(data)
    if (!retVal) return errorHandler('getStyleHead Error')
    console.log('getStyleHead sucess')
    return {
        data: retVal[1],
        index: retVal.index
    }
}

function getType(data) {
    const retVal = typeReg.exec(data)
    let type = 'css'
    if (retVal) type = retVal[1]
    console.log('getType sucess')
    return {
        data: type
    }
}

function getStyleContent(data) {
    const retVal = styleContentReg.exec(data)
    if (!retVal) return errorHandler('getStyleContent Error')
    console.log('getStyleContent sucess')
    return {
        data: retVal[1],
        index: retVal.index
    }
}

function createFile(dirInfo, filename, data) {
    fs.writeFile(`${path.join(dirInfo, filename)}`, data, function(err){
        if(err) return errorHandler(`createFile error：${err}`)
    })
}

function readDir(dirInfo) {
    fs.readdir(dirInfo, { withFileTypes: true }, function(err, files){
        if (err) return errorHandler(`readdir error：${err}`)
        files.forEach(function(item){
            if (item.isFile()){
                const temp = item.name.match(/\.([^\.]*)$/)
                if(temp && temp[1] && temp[1].toLowerCase() === 'vue') {
                    readFile(`${path.join(dirInfo, item.name)}`)
                }
            } else if (item.isDirectory()) {
                readDir(`${path.join(dirInfo, item.name)}`)
            }
        })
    })
}

function errorHandler(err) {
    console.error('errorHandler err', err)
}

if (process.argv.length > 2) {
    const dirInfo = process.argv[2]
    console.log(dirInfo)
    if (dirInfo) {
        // readFile('C:/Users/Win7/Desktop/1/confirmOrder/index.vue')
        readDir(dirInfo)
    }
}